class Solution {
    public boolean isMatch(String s, String p) {
        int n=s.length();
        int m=p.length();

        boolean[][] dp=new boolean[n+1][m+1];
        dp[0][0]=true;

        for (int i = 0; i < m; i++) {
            //当s是空串的时候
            if(p.charAt(i)=='*'){
                dp[0][i+1]=true;
            }else{
                break;
            }
        }

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if(p.charAt(j)=='*'){
                    dp[i+1][j+1]=dp[i][j+1] || dp[i+1][j];
                }else if(p.charAt(j)=='?'){
                    dp[i+1][j+1]=dp[i][j];
                }else{
                    if(s.charAt(i)==p.charAt(j)){
                        dp[i+1][j+1]=dp[i][j];
                    }
                }
            }
        }
        return dp[n][m];
    }
}